package com.hjx.binary;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnels;
import org.apache.commons.io.FileUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.stream.FileImageOutputStream;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
import java.util.stream.Stream;

public class ImgToStr {


    /**
     * 十六进制字节转字节数组
     * Creates byte array representation of HEX string.<br>
     * http://www.cnblogs.com/qgc88
     *
     * @param s string to parse
     * @return
     */
    public static byte[] fromHexString(String s) {
        int length = s.length() / 2;
        byte[] bytes = new byte[length];
        for (int i = 0; i < length; i++) {
            bytes[i] = (byte) ((Character.digit(s.charAt(i * 2), 16) << 4) | Character
                    .digit(s.charAt((i * 2) + 1), 16));
        }
        return bytes;
    }

    /**
     * Creates HEX String representation of supplied byte array.<br/>
     * Each byte is represented by a double character element from 00 to ff
     * 实现字节数组向十六进制的转换方法一 http://www.cnblogs.com/qgc88
     *
     * @param fieldData to be tringed
     * @return
     */
    public static String toHexString(byte[] fieldData) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < fieldData.length; i++) {
            int v = (fieldData[i] & 0xFF);
            if (v <= 0xF) {
                sb.append("0");
            }
            sb.append(Integer.toHexString(v));
        }
        return sb.toString();
    }

    //byte数组转图片 http://www.cnblogs.com/qgc88
    public static void byte2image(byte[] data, String path) {
        if (data.length < 3 || path.equals("")) return;
        try {
            FileImageOutputStream imageOutput = new FileImageOutputStream(new File(path));
            imageOutput.write(data, 0, data.length);
            imageOutput.close();
            System.out.println("Make Picture success,Please find image in " + path);
        } catch (Exception ex) {
            System.out.println("Exception: " + ex);
            ex.printStackTrace();
        }
    }


    public static void main(String[] args) {
//        String str2 = "ffd8ffe000104a46494600010100000100010000f****ee3fdf4ff00d0857927f0cbf43fccd483";
//        byte[] t = fromHexString(str2);
//        byte2image(t, "1.jpg");//转成保存图片


        /********************图片转十六进制******************************/
        try {
            StringBuffer sb = new StringBuffer();
            FileInputStream fis = new FileInputStream("/Users/houjinxin/Documents/edmunds/res/USEDCAR/VOLKSWAGEN/TOUAREG-HYBRID/volkswagen_touareg_2014_hybrid_used_index.jpg");
            BufferedInputStream bis = new BufferedInputStream(fis);
            java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
            byte[] buff = new byte[1024];
            int len = 0;
            while ((len = fis.read(buff)) != -1) {
                bos.write(buff, 0, len);
            }
            // 得到图片的字节数组
            byte[] result = bos.toByteArray();
            System.out.println("++++" + toHexString(result));
            // 字节数组转成十六进制

            String str = toHexString(result);
            //String str = byte2HexStr(result);
            /* http://www.cnblogs.com/qgc88
             * 将十六进制串保存到txt文件中
             */
            PrintWriter pw = new PrintWriter(new FileWriter("car.txt"));
            pw.println(str);
            pw.close();
        } catch (IOException e) {
            e.printStackTrace();
            e.printStackTrace();
        }
    }

    private static final Logger logger = LoggerFactory.getLogger(ImgToStr.class);

    @Test
    public void getCarTypeImg() throws Exception {
        System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", "50");
        BloomFilter<String> bloomFilter = BloomFilter.create(Funnels.stringFunnel(Charset.forName("UTF-8")), 100955, 0.00001);
        //url -> htmlPath
        Stream<String> stream = Stream.of("/Users/houjinxin/Documents/edmunds/res/NEWCAR/newCarType.json", "/Users/houjinxin/Documents/edmunds/res/USEDCAR/usedCarType.json");
        stream.forEach(jsonFilePath -> {

            try {
                final File file = new File(jsonFilePath.substring(0, jsonFilePath.lastIndexOf("/")) + "/hasDownloadIndexImg.txt");
                if(!file.exists()){
                    file.createNewFile();
                }
                FileUtils.readLines(file, "UTF-8").forEach(s -> bloomFilter.put(s));
                JSONArray array = JSON.parseArray(FileUtils.readFileToString(new File(jsonFilePath), "UTF-8"));
                array.stream().forEach(obj -> {
                    JSONObject allConfig = (JSONObject) obj;
                    final String brandName = allConfig.getString("brandName");
                    allConfig.getJSONArray("carTypes").stream().forEach(obj1 -> {
                        JSONObject carType = (JSONObject) obj1;
                        final String carTypeName = carType.getString("carTypeName");

                        Consumer<Object> deleteJPG = obj2 -> {

                            String link = (String) obj2;
                            String path = linkToIndexHtmlFullPathName(link, jsonFilePath, brandName, carTypeName);
                            String deleteFilePath = path.replaceAll(".html", ".jpg");
                            try {
                                File deleteFile = new File(deleteFilePath);
                                if(deleteFile.exists()){
                                    byte[] fileByte = FileUtils.readFileToByteArray(deleteFile);
                                    String imgHexStr = toHexString(fileByte);
                                    if(imgHexStr.startsWith("ffd8") && imgHexStr.endsWith("ffd9")){
                                        logger.info("正确的{}", deleteFilePath);
                                    } else {
                                        logger.info("错误的{}", deleteFilePath);
                                        new File(deleteFilePath).deleteOnExit();
                                    }


                                } else {
                                    logger.info("文件不存在 {}", deleteFilePath);
                                }

                            } catch (IOException e) {
                                e.printStackTrace();
                            }




                        };
                        carType.getJSONArray("carDetailLinks")
                                .parallelStream()
                                .forEach(deleteJPG);
                    });

                });

            } catch (IOException e) {
                e.printStackTrace();
            }
        });

    }

    private String linkToIndexHtmlFullPathName(String link, String jsonFilePath, String brandName, String carTypeName) {
        String filePath = jsonFilePath.substring(0, jsonFilePath.lastIndexOf("/")) + "/" + brandName + "/" + carTypeName.replaceAll(" ", "-") + "/";
        String fileName = link.replaceAll("https://www.edmunds.com/", "").replaceAll("/", "_") + "index.html";
        String fullPath = filePath + fileName;
        return fullPath;
    }

}
